import { ElMessage } from "element-plus";
import { ElMessageBox } from 'element-plus';

function msg(message,config){
	config = config?config:{}
	ElMessage({
		type:"info",
		message:message,
		dangerouslyUseHTMLString:config.dangerouslyUseHTMLString
	});
}
function msgSuccess(message,config){
	config = config?config:{}
	ElMessage({
		type:"success",
		message:message,
		dangerouslyUseHTMLString:config.dangerouslyUseHTMLString
	});
}
function msgWarn(message,config){
	config = config?config:{}
	ElMessage({
		type:"warning",
		message:message,
		dangerouslyUseHTMLString:config.dangerouslyUseHTMLString
	});
}
function msgError(message,config){
	config = config?config:{}
	ElMessage({
		type:"error",
		message:message,
		dangerouslyUseHTMLString:config.dangerouslyUseHTMLString
	});
}

// msgBox - 成功提示
function msgBox(content,confirmEvent,cancelEvent) {
	ElMessageBox({
		title:"信息提示",
		message:content,
		type:"info",
		showClose:false,
		confirmButtonText:"确认",
		showCancelButton:isNotNull(cancelEvent),
		cancelButtonText:isNotNull(cancelEvent)?"取消":"",
	}).then((data)=>{
		if(isNotNull(confirmEvent)){
			confirmEvent()
		}
	}).catch(()=>{
		if(isNotNull(cancelEvent)){
			cancelEvent()
		}
	})
}

// msgBox - 成功提示
function msgBoxSuccess(content,confirmEvent,cancelEvent) {
	ElMessageBox({
		title:"成功提示",
		message:content,
		type:"success",
		showClose:false,
		confirmButtonText:"确认",
		showCancelButton:isNotNull(cancelEvent),
		cancelButtonText:isNotNull(cancelEvent)?"取消":"",
	}).then((data)=>{
		if(isNotNull(confirmEvent)){
			confirmEvent()
		}
	}).catch(()=>{
		if(isNotNull(cancelEvent)){
			cancelEvent()
		}
	})
}

// msgBox - 警告提示
function msgBoxWarn(content,confirmEvent,cancelEvent) {
    ElMessageBox({
    	title:"警告提示",
    	message:content,
    	type:"warning ",
    	showClose:false,
    	confirmButtonText:"确认",
    	showCancelButton:isNotNull(cancelEvent),
    	cancelButtonText:isNotNull(cancelEvent)?"取消":"",
    }).then((data)=>{
    	if(isNotNull(confirmEvent)){
    		confirmEvent()
    	}
    }).catch(()=>{
    	if(isNotNull(cancelEvent)){
    		cancelEvent()
    	}
    })
}

// 展示模态 - 危险提示
function msgBoxError(content,confirmEvent,cancelEvent) {
    ElMessageBox({
    	title:"危险提示",
    	message:content,
    	type:"error",
    	showClose:false,
    	confirmButtonText:"确认",
    	showCancelButton:isNotNull(cancelEvent),
    	cancelButtonText:isNotNull(cancelEvent)?"取消":"",
    }).then((data)=>{
    	if(isNotNull(confirmEvent)){
    		confirmEvent()
    	}
    }).catch(()=>{
    	if(isNotNull(cancelEvent)){
    		cancelEvent()
    	}
    })
}

// 展示模态 - 删除提示
function msgBoxDel(confirmEvent,message) {
    ElMessageBox({
    	title:"危险提示",
    	message:isBlank(message)?"确认要删除吗?":message,
    	type:"error",
    	showClose:false,
    	confirmButtonText:"确认",
    	showCancelButton:true,
    	cancelButtonText:"取消",
    }).then((data)=>{
    	if(isNotNull(confirmEvent)){
    		confirmEvent()
    	}
    }).catch(()=>{})
}


/**
 * 是否不为空
 * @param {Object} params
 */
function isNull(str){
      if (typeof(str) === 'undefined'){
		  return true
	  }else if(str == null){
		  return true
	  }
	  return false
}

/**
 * 是否不为空
 * @param {Object} params
 */
function isNotNull(params){
      if (typeof(params) === 'undefined'){
		  return false
	  }else if(params == null){
		  return false
	  }
	  
	  return true
}

/**
 * 字符串是否为空
 * @param {Object} params
 */
function isBlank(str){
      if (typeof(str) === 'undefined'){
		  return true
	  }else if(str == null){
		  return true
	  }else if(str == ''){
		  return true
	  }
	  return false
}

/**
 * 字符串是否为空
 * @param {Object} params
 */
function isNotBlank(str){
	  return !isBlank(str)
}

/**
 * 获取浏览器
 */
function getExplorer() {
    var explorer = window.navigator.userAgent,
        compare = function (s) {
            return (explorer.indexOf(s) >= 0);
        },
        ie11 = (function () {
            return ("ActiveXObject" in window)
        })();
    if (compare("MSIE") || ie11) {
        return 'ie';
    } else if (compare("Firefox") && !ie11) {
        return 'Firefox';
    } else if (compare("Chrome") && !ie11) {
        if (explorer.indexOf("Edge") > -1) {
            return 'Edge';
        } else {
            return 'Chrome';
        }
    } else if (compare("Opera") && !ie11) {
        return 'Opera';
    } else if (compare("Safari") && !ie11) {
        return 'Safari';
    }
}

/**
 * 获取屏幕宽高
 */
function getScreenInfo(){
	return {
	  width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,//兼容性获取屏幕宽度
	  height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight//兼容性获取屏幕高度
	};
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
function handleTree(data, id, parentId, children) {
  let config = {
    id: id || 'id',
    parentId: parentId || 'pid',
    childrenList: children || 'children'
  };

  var childrenListMap = {};
  var nodeIds = {};
  var tree = [];

  for (let d of data) {
    let parentId = d[config.parentId];
    if (childrenListMap[parentId] == null) {
      childrenListMap[parentId] = [];
    }
    nodeIds[d[config.id]] = d;
    childrenListMap[parentId].push(d);
  }

  for (let d of data) {
    let parentId = d[config.parentId];
    if (nodeIds[parentId] == null) {
      tree.push(d);
    }
  }

  for (let t of tree) {
    adaptToChildrenList(t);
  }

  function adaptToChildrenList(o) {
    if (childrenListMap[o[config.id]] !== null) {
      o[config.childrenList] = childrenListMap[o[config.id]];
    }
    if (o[config.childrenList]) {
      for (let c of o[config.childrenList]) {
        adaptToChildrenList(c);
      }
    }
  }
  return tree;
}
function getFileType(fileName) {
	if(fileName.includes("/")){
		fileName = fileName.split("/")[fileName.split("/").length-1]
	}
	// 获取文件后缀
	let suffix = '';
	let result = '';
	if (!fileName) return "other";
	try {
		// 截取文件后缀并转换为小写
		suffix = fileName.substr(fileName.lastIndexOf('.') + 1, fileName.length).toLowerCase();
	} catch (err) {
		suffix = '';
	}
	// 如果没有后缀，返回false
	if (!suffix) {
		result = false;
		return result;
	}
	// 定义文件类型列表
	let fileTypeList = [{
			typeName: 'img',
			types: ['png', 'jpg', 'jpeg', 'bmp', 'gif']
		},
		{
			typeName: 'txt',
			types: ['txt']
		},
		{
			typeName: 'excel',
			types: ['xls', 'xlsx']
		},
		{
			typeName: 'word',
			types: ['doc', 'docx']
		},
		{
			typeName: 'pdf',
			types: ['pdf']
		},
		{
			typeName: 'ppt',
			types: ['ppt']
		},
		{
			typeName: 'video',
			types: ['mp4', 'm2v', 'mkv']
		},
		{
			typeName: 'radio',
			types: ['mp3', 'wav', 'wmv']
		}
	];
	// 匹配文件类型
	for (let i = 0; i < fileTypeList.length; i++) {
		const fileTypeItem = fileTypeList[i];
		const typeName = fileTypeItem.typeName;
		const types = fileTypeItem.types;
		result = types.some(function(item) {
			return item === suffix;
		});
		if (result) {
			return typeName;
		}
	}
	return 'other';
}


let common = {
	name:"common",
	
	msg:msg,
	msgSuccess:msgSuccess,
	msgWarn:msgWarn,
	msgError:msgError,
	
	msgBox:msgBox,
	msgBoxSuccess:msgBoxSuccess,
	msgBoxWarn:msgBoxWarn,
	msgBoxError:msgBoxError,
	msgBoxDel:msgBoxDel,
	
	isNull:isNull,
	isNotNull:isNotNull,
	isBlank:isBlank,
	isNotBlank:isNotBlank,
	
	getScreenInfo:getScreenInfo,
	
	handleTree:handleTree,
	
	getFileType:getFileType
}
export default common